import 'package:flutter/services.dart';

class HiFlutterBridge {
  static HiFlutterBridge _instance = HiFlutterBridge._();

  MethodChannel _bridge = const MethodChannel("HiFlutterBridge"); //要与native端一致

  var _listener = {};
  var header;
  HiFlutterBridge._() {
    _bridge.setMethodCallHandler((MethodCall call) {
      String method = call.method;
      if (_listener[method] != null) {
        return _listener[method](call);
      }
      return null;
    });
  }

  static HiFlutterBridge getInstance() {
    return _instance;
  }

  register(String method, Function(MethodCall) callback) {
    _listener[method] = callback;
  }

  unRegister(String method) {
    _listener.remove(method);
  }

  goToNative(Map params) {
    _bridge.invokeMethod('goToNative', params);
  }
  onBack(Map params){
    _bridge.invokeMethod('onBack',params);
  }
  Future<Map<String,String>> getHeaderParams() async{
    Map header=await _bridge.invokeMethod("getHeaderParams",{});
    return this.header=Map<String,String>.from(header);
  }

  MethodChannel bridge() {
    return _bridge;
  }
}
